package net.xinqushi.design.templateMethod.core;


import net.xinqushi.design.templateMethod.message.CustomizableMessage;
import net.xinqushi.design.templateMethod.message.Message;
import net.xinqushi.design.templateMethod.message.SMSMessage;
import net.xinqushi.design.templateMethod.message.WAPMessage;

public abstract class AbstractMessageSender implements MessageSender {

    protected volatile String username;

    protected volatile String password;

    public AbstractMessageSender(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    protected String prepare(Message message) throws Exception {
        if (username == null || password == null) {
            throw new Exception("账号或密码为空！");
        }
        if (message == null) {
            throw new Exception("发送消息为空！");
        }
        StringBuilder realMessageBuilder = new StringBuilder();
        realMessageBuilder.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        realMessageBuilder.append("<message>");
        realMessageBuilder.append("<head>");
        realMessageBuilder.append("<user>" + username + "</user>");
        realMessageBuilder.append("<password>" + password + "</password>");
        realMessageBuilder.append("</head><body>");
        realMessageBuilder.append("<to>" + message.getTo() + "</to>");
        realMessageBuilder.append("<from>" + message.getFrom() + "</from>");
        if (message instanceof WAPMessage) {
            realMessageBuilder.append("<type>wap</type>");
        } else if (message instanceof SMSMessage) {
            if (((SMSMessage) message).isLongSMS())
                realMessageBuilder.append("<type>lsms</type>");
            else
                realMessageBuilder.append("<type>sms</type>");
        }
        realMessageBuilder.append("<priority>" + message.getPriorityLevel() + "</priority>");
        if (this instanceof SchedulerPoolableSocketMessageSender) {
            realMessageBuilder.append("<schedulerEnabled>" + ((SchedulerPoolableSocketMessageSender) this).isEnabled() + "</schedulerEnabled>");
        } else {
            realMessageBuilder.append("<schedulerEnabled>false</schedulerEnabled>");
        }
        realMessageBuilder.append("<info>");
        realMessageBuilder.append("<item>");
        realMessageBuilder.append("<id>content</id>");
        realMessageBuilder.append("<type>text/plain</type>");
        realMessageBuilder.append("<value><![CDATA[" + message.getContent() + "]]></value>");
        realMessageBuilder.append("</item>");
        if (message instanceof CustomizableMessage) {
            realMessageBuilder.append("<item>");
            realMessageBuilder.append("<id>customid</id>");
            realMessageBuilder.append("<type>text/plain</type>");
            realMessageBuilder.append("<value><![CDATA[" + ((CustomizableMessage) message).getCustomID() + "]]></value>");
            realMessageBuilder.append("</item>");
        }
        if (message instanceof WAPMessage) {
            realMessageBuilder.append("<item>");
            realMessageBuilder.append("<id>pushurl</id>");
            realMessageBuilder.append("<type>text/plain</type>");
            realMessageBuilder.append("<value><![CDATA[" + ((WAPMessage) message).getUrl() + "]]></value>");
            realMessageBuilder.append("</item>");
        }
        realMessageBuilder.append("</info></body></message>");
        return realMessageBuilder.toString();
    }

    public ResponseDescriptor send(Message message) {
        String respMessage = "402";
        try {
            String realMessage = prepare(message);
            onPreSent();
            respMessage = messageSent(realMessage);
            onPostSent();
        } catch (Exception e) {
            return new ResponseDescriptor("402");
        }
        return new ResponseDescriptor(respMessage);
    }

    /**
     *  模板方法设计模式  TemplateMethod Pattern ，共有的抽象起来，方法实现延迟到子类
     *
     */
    protected abstract void onPreSent() throws Exception;

    protected abstract String messageSent(String message) throws Exception;

    protected abstract void onPostSent() throws Exception;

}
